package com.company.algo.niuke;

import com.company.algo.pojo.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 分别按照二叉树先序，中序和后序打印所有的节点。
 */
public class BT_Traversal {
    public int[][] threeOrders (TreeNode root) {
        //preOrder
        List<Integer> preList = new ArrayList<Integer>();
        preOrder(root,preList);
        int[] pre = list2Arr(preList);
        //midOrder
        List<Integer> midList = new ArrayList<Integer>();
        midOrder(root,midList);
        int[] mid = list2Arr(midList);
        //postOrder
        List<Integer> postList = new ArrayList<Integer>();
        postOrder(root,postList);
        int[] post = list2Arr(postList);
        int[][] res = new int[3][];
        res[0] = pre;
        res[1] = mid;
        res[2] = post;
        return res;
    }
    private void preOrder(TreeNode root,List<Integer> list){
        if(root==null) return;
        list.add(root.val);
        preOrder(root.left,list);
        preOrder(root.right,list);
    }

    private void midOrder(TreeNode root,List<Integer> list){
        if(root==null) return;
        midOrder(root.left,list);
        list.add(root.val);
        midOrder(root.right,list);
    }
    private void postOrder(TreeNode root,List<Integer> list){
        if(root==null) return;
        postOrder(root.left,list);
        postOrder(root.right,list);
        list.add(root.val);
    }
    private int[] list2Arr(List<Integer> list){
        int[] arr = new int[list.size()];
        for(int i=0;i<list.size();i++){
            arr[i] = list.get(i);
        }
        return arr;
    }
}
